package com.atguigu.juc.lock;

import java.util.concurrent.TimeUnit;

/**
 * @Description: 死锁案例
 * @author： yeswater
 * @create： 2024/5/29
 */

public class DeadLockDemo {
	public static void main(String[] args) {
		Object o1 = new Object();
		Object o2 = new Object();
		
		new Thread(() -> {
			synchronized (o1) {
				System.out.println(Thread.currentThread().getName() + " get o1");
				try {
					TimeUnit.SECONDS.sleep(10);
				} catch (InterruptedException e) {
					throw new RuntimeException(e);
				}
				synchronized (o2) {
					System.out.println(Thread.currentThread().getName() + " get o2");
				}
			}
		}, "t1").start();
		
		new Thread(() -> {
			synchronized (o2) {
				System.out.println(Thread.currentThread().getName() + " get o2");
				synchronized (o1) {
					System.out.println(Thread.currentThread().getName() + " get o1");
				}
			}
		}, "t2").start();
	}
}
